package com.cx.bank.controller;

import com.cx.bank.mapper.UserMapper;
import com.cx.bank.pojo.UserBean;
import com.cx.bank.service.LoginService;
import com.cx.bank.service.UserService;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    //
    @RequestMapping("/inquiry")
    public String inquiry(Model model, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        model.addAttribute("userName",userBean.getUserName());
        model.addAttribute("money", userService.inquiry(userBean));
        return "user/inquiry";
    }
    @RequestMapping("/user/index")
        public String index(Model model, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        model.addAttribute("userName",userBean.getUserName());
        return "user/index";
    }
    @RequestMapping("/user/deposit")
        public String deposit(Model model, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        model.addAttribute("userName",userBean.getUserName());
        return "user/deposit";
    }
    @RequestMapping("/user/doDeposit")
        public String doDeposit(@RequestParam("money") int money, Model model, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        System.out.println(money);
        userService.deposit(userBean,money);
        model.addAttribute("userName",userBean.getUserName());
        model.addAttribute("msg","转账成功");
        return "user/deposit";
    }
    @RequestMapping("/user/transfer")
        public String transfer(Model model, HttpSession session){
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        List<UserBean> userBeans = userMapper.findUsers();
        model.addAttribute("userBeans",userBeans);
        model.addAttribute("msg","");
        return "user/transfer";
    }
    @RequestMapping("/user/doTransfer")
        public String doTransfer(@RequestParam("userName")String userName,@RequestParam("money")String money, Model model, HttpSession session){
        List<UserBean> userBeans = userMapper.findUsers();
        model.addAttribute("userBeans",userBeans);
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        System.out.println(userName+money);
        if (money.equals("")){
            model.addAttribute("msg", "请输入金额！");
            return "user/transfer";
        }
        if (userBean.getUserName().equals(userName)){
            model.addAttribute("msg", "不能向自己转账！");
            return "user/transfer";
        }
        if (userService.transfer(userBean,userName, Double.parseDouble(money))) {
            model.addAttribute("userName", userBean.getUserName());
            model.addAttribute("msg", "转账成功！");
        }else {
            model.addAttribute("msg", "转账失败，对方账户已冻结！");
        }
        return "user/transfer";
    }
    @RequestMapping("/user/withDrawls")
        public String withDrawls(Model model, HttpSession session){
        model.addAttribute("msg","");
        return "user/withDrawls";
    }
    @RequestMapping("/user/doWithDrawls")
        public String doWithDrawls(@RequestParam("money")String money,Model model, HttpSession session) throws InvalidDepositException, AccountOverDrawnException, IOException {
        UserBean userBean = (UserBean) session.getAttribute("currentUser");
        if (userService.withdrawals(userBean, Integer.parseInt(money))){
            model.addAttribute("msg","取款成功！");
        }else {
            model.addAttribute("msg","余额不择，取款失败！");
        }
        return "user/withDrawls";
    }
    //冻结用户
    @RequestMapping("/user/freezeUser")
        public String freezeUser(Model model, HttpSession session){
        List<UserBean> userBeans = userService.findUsers();
        model.addAttribute("userBeans",userBeans);
        model.addAttribute("msg","");
        return "user/freezeUser";
    }
    @RequestMapping("/user/doFreezeUser/{state}/{userName}")
        public String doFreezeUser(@PathVariable("userName")String userName,@PathVariable("state") int state, Model model, HttpSession session ){
        List<UserBean> userBeans = userService.findUsers();
        model.addAttribute("userBeans",userBeans);
        UserBean userBean = new UserBean(userName);
        System.out.println(userName+state);
        userBean.setState(state);
        userService.setState(userBean);

        model.addAttribute("msg","");
        return "redirect:/user/freezeUser";
    }
}
